package com.lzs.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Area;
import com.lzs.PO.Menu;
import com.lzs.PO.Page;
import com.lzs.Service.AreaService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/area")
public class AreaController {
	@Autowired
	AreaService areaService;

	@RequestMapping(value = "/area_list")
	public String area_list(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Area> page = this.areaService.findAreaPagelist(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		return "/area/area_list";
	}

	@RequestMapping(value = "/area_add")
	public String areaEdit() {

		return "/area/area_add";
	}

	@ResponseBody
	@RequestMapping(value = "/areadata_load")
	public Object areadataLoad(String area_id) {
		System.out.println(area_id);
		// 构造顶级节点
		Area parent = new Area();

		parent.setArea_id("0");

		// 调用方法递归查询权限数据

		this.areaService.queryChildAreas(parent, area_id);

		return parent.getChildren();
	}

	@ResponseBody
	@RequestMapping(value = "/area_check")
	public Msg areaCheck(String area_id) {
		Boolean b = this.areaService.countAreaById(area_id);
		if (b) {
			return Msg.success();
		} else {
			return Msg.fail();
		}

	}

	@ResponseBody
	@RequestMapping(value = "/area_add_action")
	public Msg addAreaAction(String area_id, String area_name, String[] father_ids, Integer father_area_rank) {

		try {

			Integer a = father_area_rank + 1;

			String area_rank = a.toString();

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("area_id", area_id);

			map.put("area_name", area_name);

			map.put("father_ids", father_ids);

			map.put("area_rank", area_rank);

			this.areaService.addArea(map);

			return Msg.success();
		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();
		}

	}

	@RequestMapping(value = "/area_update")
	public String updateArea(String area_id, Model model) {

		Area area = this.areaService.selectAreaById(area_id);

		Area father_area = this.areaService.selectFatherAreaById(area.getFather_id());

		model.addAttribute("area", area);

		model.addAttribute("fatherarea", father_area);

		return "/area/area_update";
	}

	@ResponseBody
	@RequestMapping(value = "/area_update_action")
	public Msg updateAreaAction(String area_id, String area_name, String father_id, Integer father_area_rank) {

		try {
			Integer a = father_area_rank + 1;

			String area_rank = a.toString();

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("area_id", area_id);

			map.put("area_name", area_name);

			map.put("father_id", father_id);

			map.put("area_rank", area_rank);
          
			this.areaService.updateArea(map);
			
			return Msg.success();

		} catch (Exception e) {
			
			e.printStackTrace();

			return Msg.fail();
		}

	}
  @ResponseBody
  @RequestMapping(value = "/area_delete")
  public Msg deleteArea(String area_id) {
	  
	  List<Area> areaList = this.areaService.selectChildrenAreaById(area_id);
	
	  if(areaList!=null && !areaList.isEmpty()) {
		  
		  return Msg.fail();
				  
	  }else {
		  
		  this.areaService.deleteArea(area_id);
		  
		  return Msg.success();
		  
	  }
	  
  }
  @ResponseBody
  @RequestMapping(value = "/area_loadArea")
  public Msg loadAreaData() {
	  try {
		  
		  List<Area> arealist = this.areaService.findAreaOfAll();
		  
		  return Msg.success().add("arealist", arealist);
		  
	  }catch(Exception e) {
		  
		  e.printStackTrace();
		  
		  return Msg.fail();
	  }
  
  }
  @ResponseBody
  @RequestMapping(value = "/area_loadAreaWithCounty")
   public Msg loadAreaWithCounty() {
	  
	  try {
		  
		  List<Area>countylist = this.areaService.findAreaWithCounty();
		  
		  return Msg.success().add("countylist", countylist);
		  
	  }catch(Exception e) {
		  
		  e.printStackTrace();
		  
		  return Msg.fail();
		  
	  }
	  
  }
  @ResponseBody
  @RequestMapping(value = "/area_loadAreaWithFatherId")
  public Msg loadAreaWithFatherId(String father_id) {
	  
	  try {
		  
		  List<Area> childarealist = this.areaService.selectChildrenAreaById(father_id);
		  
		  return Msg.success().add("childarealist", childarealist);
		  
	  }catch(Exception e) {
		  
		  e.printStackTrace();
		  
		  return Msg.fail();
		  
	  }
	  
	  
  }
}
